# conding=utf-8
import sys
import pandas as pd
from paddleocr import PaddleOCR



ocr = PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=True,show_log=False,code='utf-8')
def get_value_by_OCR(img_path,is_and=False,*match_str):
    '''
    将图片中的文字识别出来，并返回所有识别的对结果
    :param img_path:识别图片
    :param is_and:用作判断是匹配字段是且，还是或
    :return:
    '''
    # 如果你的机器有 GPU 并且安装了 paddlepaddle-gpu，可以添加 use_gpu=True 参数来启用 GPU 加速
    ret = []
    result = ocr.ocr(img_path, cls=True)  # cls=True 表示启用方向分类器
    ocr_all_datas = result[0]
    if match_str:
        for i in range(len(ocr_all_datas)):
            wenzi = ocr_all_datas[i][1][0]
            if is_and:
                fagle = []
                for agr in match_str:
                    if agr in wenzi:    # 都要满足
                        fagle.append(True)
                    else:
                        fagle.append(False)
                if all(fagle):
                    ret.append(ocr_all_datas[i])
            else:
                for agr in match_str:
                    if agr in wenzi:    # 满足其一即可
                        ret.append(ocr_all_datas[i])
                        break
        return ret
    else:

        return ocr_all_datas

def answer_sanjie(question_img,answer_img,xls_path):
    '''

    :param question_img: 含问题的图片
    :param answer_img: 含答案的图片
    :return:
    '''
    # 1.截图,区域识别，获取问题
    ret_question = get_value_by_OCR(question_img,True,)  # 根据题目位置，调节识别区域
    if ret_question:
        question_title = ""
        for i in range(len(ret_question)):
            question_title = question_title+ ret_question[i][1][0]
        print(f"该题是：{question_title}")
        # 2.处理数据获取坐标
        keywords = ""
        df = pd.read_excel(xls_path)
        excel_all_data = df.values
        index = 0  # 用于查找excel中的几行
        
        for j in range(len(excel_all_data)):
            keyword = excel_all_data[j][1]
            if "，" in keyword:
                keywords = str(keyword).split("，")
            else:
                keywords = [keyword]
            flage = []
            for kw in keywords:
                if kw in question_title:
                    flage.append(True)
                else:
                    flage.append(False)
            if all(flage) and flage:
                index = j
                print(f"根据【关键字：{keyword}】获取的文字信息：{question_title}")


        if index !=0:
            # 3 从表格中获取答案
            answer = excel_all_data[index][2]
            # 4 根据表格答案分割
            if "，" in answer:
                answers = str(answer).split("，")
            else:
                answers = [answer]
            # 5 作答
            for k in range(len(answers)):
                ret_answer = get_value_by_OCR(answer_img,False,answers[k])  # 根据题目位置，调节识别区域
                for i in range(len(ret_answer)):
                    print(f"识别答案是：{ret_answer[i][1][0]}")
                print(f"该题答案是：{answers[k]}")
                if ret_answer:
                    center_x = int((ret_answer[0][0][1][0] + ret_answer[0][0][0][0])/2)
                    center_y = int((ret_answer[0][0][3][1] + ret_answer[0][0][1][1])/2)
                    print(f"坐标|{center_x}|{center_y}")  
                    return
        else:  
            print("未识别答案")
            print(f"坐标|20|50")
            
    else:
        print("没有题目")


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: python script_name.py <image_path> [<match_str1> <match_str2> ...]")
        sys.exit(1)

    img_path = sys.argv[1]
    answer_path = sys.argv[2]
    xls_path = sys.argv[3]
    answer_sanjie(img_path,answer_path,xls_path)


